<!DOCTYPE html>

<!--
-->

<html>
<!--
Copyright 2013 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Test for goog.date.duration</title>
<script src="../base.js"></script>
<script>
  goog.LOCALE = 'en_US';
  goog.require('goog.date.duration');
  goog.require('goog.i18n.DateTimeFormat');
  goog.require('goog.i18n.DateTimeSymbols');
  goog.require('goog.i18n.DateTimeSymbols_bn'); // Bengali
  goog.require('goog.i18n.DateTimeSymbols_en');
  goog.require('goog.i18n.DateTimeSymbols_fa'); // Persian
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
  var MINUTE_MS = 60000;
  var HOUR_MS = 60 * MINUTE_MS;
  var DAY_MS = 24 * HOUR_MS;

  var duration = goog.date.duration.format;

  function testFormatDurationZeroMinutes() {
    assertEquals('0 minutes', duration(0));
    assertEquals('0 minutes', duration(MINUTE_MS - 1));
  }

  function testFormatDurationMinutes() {
    assertEquals('1 minute', duration(MINUTE_MS));
    assertEquals('1 minute', duration(MINUTE_MS + 1));
    assertEquals('5 minutes', duration(5 * MINUTE_MS));
    assertEquals('45 minutes', duration(45 * MINUTE_MS));
  }

  function testFormatDurationHours() {
    assertEquals('1 hour', duration(HOUR_MS));
    assertEquals('1 hour', duration(HOUR_MS + 1));
    assertEquals('1 hour 1 minute', duration(HOUR_MS + MINUTE_MS));
    assertEquals('1 hour 45 minutes', duration(HOUR_MS + 45 * MINUTE_MS));

    assertEquals('5 hours', duration(5 * HOUR_MS));
    assertEquals('5 hours', duration(5 * HOUR_MS + 1));
    assertEquals('5 hours 1 minute', duration(5 * HOUR_MS + MINUTE_MS));
    assertEquals('5 hours 45 minutes', duration(5 * HOUR_MS + 45 * MINUTE_MS));

    assertEquals('11 hours', duration(11 * HOUR_MS));
    assertEquals('11 hours', duration(11 * HOUR_MS + 1));
    assertEquals('11 hours 1 minute', duration(11 * HOUR_MS + MINUTE_MS));
    assertEquals('11 hours 45 minutes',
        duration(11 * HOUR_MS + 45 * MINUTE_MS));
  }

  function testFormatDurationDays() {
    assertEquals('1 day', duration(DAY_MS));
    assertEquals('1 day', duration(DAY_MS + 1));
    assertEquals('1 day 1 minute', duration(DAY_MS + MINUTE_MS));
    assertEquals('1 day 45 minutes', duration(DAY_MS + 45 * MINUTE_MS));
    assertEquals('1 day 1 hour', duration(DAY_MS + HOUR_MS));
    assertEquals('1 day 11 hours', duration(DAY_MS + 11 * HOUR_MS));
    assertEquals('1 day 1 hour 1 minute',
        duration(DAY_MS + HOUR_MS + MINUTE_MS));
    assertEquals('1 day 1 hour 45 minutes',
        duration(DAY_MS + HOUR_MS + 45 * MINUTE_MS));
    assertEquals('1 day 11 hours 1 minute',
        duration(DAY_MS + 11 * HOUR_MS + MINUTE_MS));
    assertEquals('1 day 11 hours 45 minutes',
        duration(DAY_MS + 11 * HOUR_MS + 45 * MINUTE_MS));

    assertEquals('11 days', duration(11 * DAY_MS));
    assertEquals('11 days', duration(11 * DAY_MS + 1));
    assertEquals('11 days 1 minute', duration(11 * DAY_MS + MINUTE_MS));
    assertEquals('11 days 45 minutes', duration(11 * DAY_MS + 45 * MINUTE_MS));
    assertEquals('11 days 1 hour', duration(11 * DAY_MS + HOUR_MS));
    assertEquals('11 days 11 hours', duration(11 * DAY_MS + 11 * HOUR_MS));
    assertEquals('11 days 1 hour 1 minute',
        duration(11 * DAY_MS + HOUR_MS + MINUTE_MS));
    assertEquals('11 days 1 hour 45 minutes',
        duration(11 * DAY_MS + HOUR_MS + 45 * MINUTE_MS));
    assertEquals('11 days 11 hours 1 minute',
        duration(11 * DAY_MS + 11 * HOUR_MS + MINUTE_MS));
    assertEquals('11 days 11 hours 45 minutes',
        duration(11 * DAY_MS + 11 * HOUR_MS + 45 * MINUTE_MS));
  }

  function testFormatDurationPersianDigits() {
    goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_fa;

    // ۱ minute
    assertEquals(localizeNumber(1) + ' minute', duration(MINUTE_MS));
    // ۲ minutes
    assertEquals(localizeNumber(2) + ' minutes', duration(2 * MINUTE_MS));
    // ۱۰ minutes
    assertEquals(localizeNumber(10) + ' hours', duration(10 * HOUR_MS));
    // ۲۳ days
    assertEquals(localizeNumber(23) + ' days', duration(23 * DAY_MS));

    // Restore to English, to make sure we don't mess up other tests
    goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en;
  }

  function testFormatDurationBengaliDigits() {
    goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_bn;

    // ১ minute
    assertEquals(localizeNumber(1) + ' minute', duration(MINUTE_MS));
    // ২ minutes
    assertEquals(localizeNumber(2) + ' minutes', duration(2 * MINUTE_MS));
    // ১০ minutes
    assertEquals(localizeNumber(10) + ' hours', duration(10 * HOUR_MS));
    // ২৩ days
    assertEquals(localizeNumber(23) + ' days', duration(23 * DAY_MS));

    // Restore to English, to make sure we don't mess up other tests
    goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en;
  }

  function localizeNumber(value) {
    // Quick conversion to national digits, to increase readability of the
    // tests below.
    return goog.i18n.DateTimeFormat.prototype.localizeNumbers('' + value);
  }

</script>
</body>
</html>
